package dbops;

import java.util.ArrayList;

import org.eclipse.hyades.execution.runtime.datapool.IDatapool;
import org.eclipse.hyades.execution.runtime.datapool.IDatapoolIterator;

import com.rational.test.ft.script.IOptionName;

import core.testcases.TestProjectSuperHelper;
import dbops.COLUMN.DATA_TYPE;
/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class DatapoolOperator extends TestProjectSuperHelper
{
	protected ArrayList<COLUMN> columns;
	
	/**
	 * Load datapool content and return iterator to the content.
	 * 
	 * @param name
	 *            Name of the datapool which has to be in Datapools folder.
	 */
	protected IDatapoolIterator loadDatapool(String name)
	{
		IDatapoolIterator dpIter;
		IDatapool dp;
		
		java.io.File dpFile = new java.io.File((String)getOption(IOptionName.DATASTORE),"Datapools/" + name + ".rftdp");
	    dp = dpFactory().load(dpFile, false);
	    
	    getColumns(dp);

	    //Use default iterator class
	    dpIter = dpFactory().open(dp, null);

	    //Use default equivalence class
	    dpIter.dpInitialize(dp);
	    
	    return dpIter;
	}
	
	/**
	 * Get all datapool columns and add them in columns array list.
	 * 
	 * @param dp
	 *            the datapool which columns will be get.
	 */
	private void getColumns(IDatapool dp)
	{
		for(int columnIdx = 0; columnIdx < dp.getVariableCount(); columnIdx++)
		{
			columns.add(new COLUMN(dp.getVariable(columnIdx).getName(),
					dp.getVariable(columnIdx).getSuggestedType().getSuggestedClassName().equals("String")?DATA_TYPE.StringType:DATA_TYPE.OtherType));
		}
	}
	
	/**
	 * Get row from the datatpool and return it in array list of strings.
	 * 
	 * @param dpIter
	 *            iterator from datapool.
	 */
	protected ArrayList<String> getDatapoolRow(IDatapoolIterator dpIter)
	{
		ArrayList<String> result = new ArrayList<String>();
		
		for(int varIdx = 0; varIdx < dpIter.getDatapool().getVariableCount(); varIdx++)
		{
			result.add(dpIter.dpString(varIdx));
		}
		
		return result;
	}
}

